﻿$(function () {

    /*评论活动切换*/
    $(".commenttab li").click(function () {
        $(".commenttab li").removeClass("active");
        $(this).addClass("active");
        var ind = $(".commenttab li").index($(this));
        $(".commentcon").removeClass("f-show").addClass("f-hide");
        $(".commenttab li").parents(".tabbable").find(".commentcon").eq(ind).removeClass("f-hide").addClass("f-show");
    });

    /*新建任务弹出层*/
    $(".addtaskbtn").click(function () {
        $(".addnewtask").show();
    });

    $(".cancel").click(function () {
        $(".addnewtask").hide();
    });

    /*任务名称编辑*/
    $(".taskname").click(function () {
        $(this).removeClass("f-show").addClass("f-hide");
        $(this).next(".taskedit").removeClass("f-hide").addClass("f-show");
    });

    $('#btnCancelTitle').click(function () {
        $(".taskname").removeClass("f-hide").addClass("f-show");
        $(".taskedit").removeClass("f-show").addClass("f-hide");
    });


    /*点任务名时，弹出右侧内容*/
    $(".right_click_trigger").live('click', function () {
        $('.comment-list').find('.part-loading').show();
        //layer.alert($(this).attr('taskid'),
        //{
        //    skin: 'layui-layer-lan', shift: 0 //动画类型
        //});
        $(".slide-content").show();
        // 根据任务Id，加载表单数据
        taskid = $(this).attr('taskid');
        $.ajax({
            type: 'post',
            url: '/task/gettask/' + taskid,
            cache: false,
            dataType: 'json',
            success: function (d) {
                showInfo(d, function (d) {
                    var task = d.data;
                    $('#spTitle').text(task.TaskTitle);
                    $('#spKind').text(tasklistkind[task.TaskListKind]);
                    $('#divDesc').text(task.TaskDesc);
                    if (task.TaskDesc.length > 0) {
                        $('#aTips').text('');
                    }
                    $('#txtTaskTitle').val(task.TaskTitle);
                    $('#txtTaskDesc').val(task.TaskDesc);
                    var str = juicer(tplComments, d.data);
                    $('.comments').html(str);
                    $('.comment-list').find('.part-loading').hide();

                    var userStr = juicer(tplUser, d.data);

                    $('.entity-members').find('.removememberbtn').remove();

                    $(userStr).insertBefore($('.entity-members').find('.assignmenttaskbtn'));
                });
            }
        });
    });

    //新建任务 txtTitle1标题，sloption列表
    $('#btnSave1').click(function () {
        var title = $('#txtTitle1').val();
        var listkind = $('#sloption').val();

        if (title == '') {
            layer.tips('请输入任务内容！', '#txtTitle1');
            return;
        }

        $('#btnSave1').attr("disabled", true);

        $.ajax({
            type: 'post',
            data: {
                TaskTitle: title,
                TaskListKind: listkind
            },
            url: '/Task/add',
            cache: false,
            dataType: 'json',
            success: function (d) {
                $('#btnSave1').attr("disabled", false);
                if (d.success) {
                    $('#txtTitle1').val('');
                    $(".addnewtask").hide();
                    // todo: 列表加载新增的任务
                    var str = juicer(tpl, d);
                    $('#list' + listkind).append(str);
                } else {
                    layer.msg(d.msg);
                }
            }
        });

    });

    //保存2，修改标题和添加任务描述
    $('#btnSave2').click(function () {
        var title = $('#txtTaskTitle').val();
        var taskdesc = $('#txtTaskDesc').val();
        $.ajax({
            type: 'post',
            url: '/task/update/',
            data: {
                KeyId: taskid,
                TaskTitle: title,
                TaskDesc: taskdesc
            },
            cache: false,
            dataType: 'json',
            success: function (d) {
                showInfo(d, function (d) {
                    $('#spTitle').text(title);
                    $('#divDesc').text(taskdesc);
                    if (taskdesc.length > 0) {
                        $('#aTips').text('');
                    } else {
                        $('#aTips').text('点击添加任务描述');
                    }
                    $(".taskname").removeClass("f-hide").addClass("f-show");
                    $(".taskedit").removeClass("f-show").addClass("f-hide");
                    //更新列表中数据
                    $('.right_click_trigger[taskid=' + taskid + ']').find('.entry-task-title').text(title);
                });
            }
        });
    });


    $(".icon-remove").click(function () {
        $(this).parents(".popbox").hide();
    });

    /*分配任务*/
    $(".assignmenttaskbtn").click(function () {
        $(this).parents().find(".assignmenttask").show();
        $('.popbox-body .part-loading').show();
        //获取用户列表
        $.ajax({
            type: 'post',
            url: '/task/GetUserNotInTask/',
            data: {
                taskid: taskid
            },
            cache: false,
            dataType: 'json',
            success: function (d) {
                showInfo(d, function (d) {
                    $('.popbox-body .part-loading').hide();
                    var str = juicer(tplUser2, d);
                    $('#userlist').html(str);
                });
            }
        });
    });

    //用户搜索change事件
    $('#userkey').live('change', function () {
        $('.popbox-body .part-loading').show();
        //获取用户列表
        $.ajax({
            type: 'post',
            url: '/task/GetUserNotInTask/',
            data: {
                taskid: taskid,
                username: $('#userkey').val()
            },
            cache: false,
            dataType: 'json',
            success: function (d) {
                showInfo(d, function (d) {
                    $('.popbox-body .part-loading').hide();
                    var str = juicer(tplUser2, d);
                    $('#userlist').html(str);
                });
            }
        });
    });

    //选中用户添加到用户分配表中
    $('#userlist .member-item').live('dblclick', function () {
        var userid = $(this).attr('userid');
        var username = $(this).find('.avatar').attr('title');
        var av = $(this).find('.avatar-text').text();
        $.ajax({
            type: 'post',
            url: '/task/AddJoinUser/',
            data: {
                TaskId: taskid,
                UserId: userid
            },
            cache: false,
            dataType: 'json',
            success: function (d) {
                showInfo(d, function (d) {
                    var data = {
                        users: [
                            {
                                UserName: username,
                                UserId: userid,
                                av: av
                            }
                        ]
                    }
                    var userStr = juicer(tplUser, data);
                    $(userStr).insertBefore($('.entity-members').find('.assignmenttaskbtn'));
                });
            }
        });

    });

    $(".icon-remove").click(function () {
        $(this).parents(".slide-content").hide();
    });

    /*设置日期*/
    $(".setdate").click(function () {
        $(this).parents().find(".setdatebox").show();
    });

    /*设置日期关闭*/
    $(".close").click(function () {
        $(this).parents().find(".popbox").hide();
    });


    /*从任务中移除*/
    //$(".removememberbtn").live('click', function () {
    //    $(this).parents().find(".removemembers").show();
    //});


    ajaxPost('/task/GetListByKind?type=0', function (d) {
        $('.entries_panel').find('.part-loading').eq(0).hide();
        var str = juicer(tpl, d);
        $('#list0').html(str);
    });
    ajaxPost('/task/GetListByKind?type=1', function (d) {
        $('.entries_panel').find('.part-loading').eq(1).hide();
        var str = juicer(tpl, d);
        $('#list1').html(str);
    });
    ajaxPost('/task/GetListByKind?type=2', function (d) {
        $('.entries_panel').find('.part-loading').eq(2).hide();
        var str = juicer(tpl, d);
        $('#list2').html(str);
    });


    $('#btnFeedBack').click(function () {
        var content = $('#txtContent').val().trim();
        if (content.length == 0) {
            layer.tips('请输入您的评论！', '#txtContent');
            return;
        }

        $('#btnFeedBack').attr("disabled", true);
        $.ajax({
            type: 'post',
            data: {
                TaskId: taskid,
                FContent: content
            },
            url: '/Task/AddFeedBack',
            cache: false,
            dataType: 'json',
            success: function (d) {
                $('#btnFeedBack').attr("disabled", false);
                if (d.success) {
                    $('#txtContent').val('');
                    var str = juicer(tplComments, d);
                    $('.comments').append(str);
                } else {
                    layer.msg(d.msg);
                }
            }
        });
    });

    initScroll();

});

var tpl = [
'{@each data as it, k}',
'<div class="right_click_trigger slide-trigger js-popbox task task_style task-first ui-droppable" taskid="${it.KeyId}">',
'<div class="entry-task-main">',
'<a class="entry-task-check"> <i class="icon-check-empty"></i> </a> <a class="entry-task-title ng-binding" href="javascript:;">${it.TaskTitle}</a>',
'<div class="ng-scope"> <a href="javascript:;" class="js-popbox entry-task-menu ng-scope"> <i class="icon-play-circle"></i> </a></div>',
'</div>',
//'<div class="task-badges"></div>',
//'<div class="entry-task-members ng-hide"> </div>',
'<div class="task-badges"> <span class="task-badge icon icon-time {@if it.over<0} badge-expire-due {@/if}" title="任务已经截止">${it.endtime}</span> </div>',
'<div class="entry-task-members"> <a class="js-popbox member avatar avatar-30" title="${it.UserName}"> <span class=""><span class="avatar-text">${it.UserName}  </span></span> </a></div>',
'</div>',
'{@/each}'
].join('\n');

//var tpl = [
//'{@each data as it, k}',
//'<div class="right_click_trigger slide-trigger js-popbox task task_style task-first ui-droppable" taskid="${it.KeyId}">',
//'<div class="entry-task-main">',
//'<a class="entry-task-check"> <i class="icon-check-empty"></i> </a> <a class="entry-task-title ng-binding" href="javascript:;">${it.TaskTitle}</a>',
//'<div class="ng-scope"> <a href="javascript:;" class="js-popbox entry-task-menu ng-scope"> <i class="icon-play-circle"></i> </a></div>',
//'</div>',
////'<div class="task-badges"></div>',
////'<div class="entry-task-members ng-hide"> </div>',
//'<div class="task-badges"> <span class="task-badge icon icon-time {@if it.over<0} badge-expire-due {@/if}" title="任务已经截止">${it.endtime}</span> </div>',
//'<div class="entry-task-members"> <a class="js-popbox member avatar avatar-30" title="${it.UserName}"> <span class="avatar-face"><span class="avatar-text">${it.UserName}</span></span> </a></div>',
//'</div>',
//'{@/each}'
//].join('\n');

var tplComments = [
    '{@each feedback as it, k}',
   '<li class="comment-item ng-scope">',
    '<a class="comment-avatar avatar avatar-40" size="40" title="${it.UserName}" href="javascript:;"> <span class="avatar-face"><span class="avatar-text">${it.UserName}</span></span> </a>',
    '<div class="comment">',
    '<a href="javascript:;" class="comment-name">${it.UserName}</a>',
    '<span class="comment-meta">${it.time} </span>',
    '<div class="current-comment ng-binding markdown">',
    '<p>${it.FContent}</p>',
    '</div>',
    '</div>',
   '</li>',
   '{@/each}'
].join('\n');

var tplUser = [
 '{@each users as it, k}',
 '<li class="removememberbtn" userid="${it.UserId}"><a class="js-popbox" title="${it.UserName}" href="javascript:;"><span class="avatar-text">${it.UserName}</span></a></li>',
  '{@/each}'
].join('\n');
//var tplUser = [
// '{@each users as it, k}',
// '<li class="removememberbtn" userid="${it.UserId}"><a class="js-popbox avatar avatar-40" title="${it.UserName}" href="javascript:;"><span class="avatar-face"><span class="avatar-text">${it.av}</span></span></a></li>',
//  '{@/each}'
//].join('\n');

var tplUser2 = [
  '{@each users as it, k}',
  '<li class="member-item ng-scope" userid="${it.UserId}"><a href="javascript:;"> <i class="color_gray ng-scope icon-xx"></i><span class="avatar avatar-30" size="30" title="${it.UserName}" href="javascript:;"> <span class="avatar-face plr_5"><span class="avatar-text">${it.av}</span></span> <span class="avatar-name">${it.UserName}</span></span> </a></li>',
  '{@/each}'
].join('\n');

function generateStr() {
    var d = {
        data: [
            { TaskDesc: "fsfsdfsdfsdfsffsdfsadfasfsadfsadfaddddddddddddddddddddddddddddddddddddddddddd", KeyId: 1, UserName: "谢旺" },
            { TaskDesc: "adfa", KeyId: 12, UserName: "张大鹏" },
            { TaskDesc: "ddfs", KeyId: 4, UserName: "谢旺" },
            { TaskDesc: "adddd", KeyId: 11, UserName: "谢旺" }
        ]
    };
    var str = juicer(tpl, d);
    $('#list0').html(str);
    $('#list1').html(str);
    $('#list2').html(str);
}

var tasklistkind = ['要做', '在做', '待定'];

function initScroll() {

    $(".entry-task-panel").mCustomScrollbar({
        scrollButtons: {
            enable: false
        },
        advanced: {
            updateOnContentResize: true
        }
    });

    $(".layout_right_sidebar").mCustomScrollbar({
        scrollButtons: {
            enable: false
        },
        advanced: {
            updateOnContentResize: true
        }
    });

    $('.tab-pane').mCustomScrollbar({
        scrollButtons: {
            enable: false
        },
        advanced: {
            updateOnContentResize: true
        }
    });

}